#!/bin/sh
##############################################################################
#  OpenDomo: Security & Domotics
#  Copyright (C) 2008  Isidro Catalan <skarvin@gmail.com>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
##############################################################################


# ----------------------------------------------------------------------------
# desc: Librearia de escritura / lectura de ficheros de configuracion con 
#		  formato:
#		  variable valor
# author: Isidro Catalan <skarvin@gmail.com>, http://www.opendomo.com
# date: October 2008
#
# CHANGES:
#
# ----------------------------------------------------------------------------

#TODO: Agregar control de errores
#PARAMS=$@
#CONF_FILE=ACTION=VAR_NAME=VAR_VALUE=""

TMP_FILE="/tmp/uskxaqki`date +%N`"

setValue ()
{
 VAR_NAME="$1"
 VAR_VALUE="$2"
 mod=0
 while read line; do

	 found=`echo $line | grep "^$VAR_NAME="`
	 
	 if [ -n "$found" ]; then
	 	echo "$VAR_NAME=\"$VAR_VALUE\"" >> $TMP_FILE
	 	mod=1
	 else 
	 	echo $line >> $TMP_FILE
	 fi 
 
 done < "$CONF_FILE"
 # Si no encontramos el valor a modificar lo añadimos como
 # linea nueva 
 if [ $mod = 0 ]; then
 	echo "$VAR_NAME=\"$VAR_VALUE\"" >> $CONF_FILE
 	rm $TMP_FILE
 else
	 # swapeamos los ficheros
 	mv "$TMP_FILE" "$CONF_FILE"
 fi
}

getValue ()
{
VAR_NAME="$1"

 while read line; do

	 found=`echo $line | grep "$VAR_NAME"`
	 
	 if [ -n "$found" ]; then
			VALUE=`echo $found | cut -d"=" -f2`
			echo "$VALUE"
	 fi 
	 
 done < "$CONF_FILE"
}



